$(function () {
    //抽取变量
    var client_datagrid = $("#client_datagrid");
    var client_form = $("#client_form");
    var client_dialog = $("#client_dialog");
    var clientQuery_dialog = $("#clientQuery_dialog");
    var clientQuery_form = $("#clientQuery_form");
//初始化datagrid
    client_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/client/list.do",
        pagination: true,
        singleSelect: true,
        rownumbers: true,
        toolbar: "#client_toolbar",
        columns: [[
            {field: 'name', title: '姓名', width: 70, align: 'center'},
            {
                field: 'saleman', title: '营销人员', width: 70, align: 'center', formatter: function (value, row, index) {
                    return value ? value.username : "";
                }
            },
            {field: 'traceNum', title: '跟踪次数', width: 60, align: 'center'},
            {field: 'lastTraceTime', title: '最后跟踪时间', width: 100, align: 'center'},
            {field: 'appointTime', title: '约访日期', width: 100, align: 'center'},
            {field: 'nextTraceTime', title: '下次跟进时间', width: 100, align: 'center'},
            {field: 'qq', title: 'QQ', width: 80, align: 'center'},
            {field: 'tel', title: '电话', width: 80, align: 'center'},
            {field: 'school', title: '学校', width: 70, align: 'center'},
            {
                field: 'intentionDegree',
                title: '意向程度',
                width: 70,
                align: 'center',
                formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'campus', title: '意向校区', width: 70, align: 'center', formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'clz', title: '意向班级', width: 70, align: 'center', formatter: function (value, row, index) {
                    return value ? value.name : "";
                }
            },
            {
                field: 'state', title: '状态', width: 70, align: 'center', formatter: function (value, row, index) {
                    if (value && value.name == '正式学员') {
                        return "<b><span style='color:green;'>正式学员</span></b>"
                    } else {
                        return value ? value.name : "";
                    }
                }
            },
            {
                field: 'isTraced', title: '是否跟踪', width: 60, align: 'center', formatter: function (value, row, index) {
                    if (row.traceNum) {
                        return '已跟踪';
                    } else {
                        return "<span style='color: red'>未跟踪</span>";
                    }
                }
            },
            {field: 'remark', title: '备注', width: 60, align: 'center'}
        ]]
    });
    //初始化dialog
    client_dialog.dialog({
        width: 700,
        height: 500,
        buttons: "#client_btns",
        closed: true
    });
    // 高级查询对话框
    clientQuery_dialog.dialog({
        width: 650,
        height: 250,
        buttons: "#clientQuery_btns",
        closed: true
    });
    //所有ab标签的方法都交给methodObject来管理
    var methodObjdect = {
        //新增潜在学员
        add: function () {
            client_form.form("clear");
            // $("#client_form input").prop("disabled", false);
            client_dialog.dialog("setTitle", "新增潜在客户");
            client_dialog.dialog("open");
        },
        edit: function () {
            client_form.form("clear");
            //获取选中行的数据
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择你要编辑的数据", 'info');
            } else {
                if (row.eduBackground_id) {
                    row["eduBackground.id"] = row.eduBackground_id;
                }
                if (row.source_id) {
                    row["source.id"] = row.source_id;
                }
                if (row.introducerStu_id) {
                    row["introducerStu.id"] = row.introducerStu_id;
                }
                if (row.clientType_id) {
                    row["clientType.id"] = row.clientType_id;
                }
                if (row.intentionDegree_id) {
                    row["intentionDegree.id"] = row.intentionDegree_id;
                }
                if (row.state_id) {
                    row["state.id"] = row.state_id;
                }
                if (row.clz_id) {
                    row["clz.id"] = row.clz_id;
                }
                if (row.bigCustomer_id) {
                    row["bigCustomer.id"] = row.bigCustomer_id;
                }
                if (row.campus_id) {
                    row["campus.id"] = row.campus_id;
                }
                if (row.intentionSubject_id) {
                    row["intentionSubject.id"] = row.intentionSubject_id;
                }
                //将查询的数据加载进编辑表单中
                client_form.form("load", row);
                client_dialog.dialog("setTitle", "编辑潜在客户");
                client_dialog.dialog("open");
            }
        },
        view: function () {
            client_form.form("clear");
            //获取选中行的数据
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择你要编辑的数据", 'info');
            } else {
                if (row.eduBackground_id) {
                    row["eduBackground.id"] = row.eduBackground_id;
                }
                if (row.source_id) {
                    row["source.id"] = row.source_id;
                }
                if (row.introducerStu_id) {
                    row["introducerStu.id"] = row.introducerStu_id;
                }
                if (row.clientType_id) {
                    row["clientType.id"] = row.clientType_id;
                }
                if (row.intentionDegree_id) {
                    row["intentionDegree.id"] = row.intentionDegree_id;
                }
                if (row.state_id) {
                    row["state.id"] = row.state_id;
                }
                if (row.clz_id) {
                    row["clz.id"] = row.clz_id;
                }
                if (row.bigCustomer_id) {
                    row["bigCustomer.id"] = row.bigCustomer_id;
                }
                if (row.campus_id) {
                    row["campus.id"] = row.campus_id;
                }
                if (row.intentionSubject_id) {
                    row["intentionSubject.id"] = row.intentionSubject_id;
                }
                //将查询的数据加载进编辑表单中
                client_form.form("load", row);
                client_dialog.dialog("setTitle", "查看潜在客户");
                $("#client_form input").prop("disabled", true);
                client_dialog.dialog("open");
            }
        },
        //新增/编辑保存操作
        save: function () {
            client_form.form("submit", {
                url: "/client/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, 'info', function () {
                            client_dialog.dialog("close");
                            client_datagrid.datagrid("reload");
                        })
                    } else {
                        $.messager.alert("温馨提示", data.msg, 'info', function () {
                            client_dialog.dialog("close");
                        })
                    }
                }
            })
        },
        //高级查询
        query: function () {
            clientQuery_form.form("clear");
            clientQuery_dialog.dialog("setTitle", "高级查询");
            clientQuery_dialog.dialog("open");
        },
        //提交查高级查询表单
        submitForm: function () {
            clientQuery_form.form("submit", {
                url: "/client/list.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    clientQuery_dialog.dialog("close");
                    //加载本地数据，旧的行将被移除
                    client_datagrid.datagrid("loadData", data);
                }
            })
        }
    };
    //所有a标签对应的事件有method来调用
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        //调用具体的方法
        methodObjdect[cmd]();
    });
});
